//
//  FeedbackDetailTableView.m
//  BOSS-SOOC
//
//  Created by apple on 16/8/26.
//  Copyright © 2016年 Gcx. All rights reserved.
//

#import "FeedbackDetailTableView.h"
#import "FeedbackDetailCell.h"
#import "UITableView+SCKit.h"
#import "OptionHeaderView.h"
#import "FeedbackDetailModel.h"
#import "OptionModel.h"

@interface FeedbackDetailTableView()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic,strong) NSMutableArray<FeedbackDetailModel*>*orders;
@property (nonatomic,strong) OptionHeaderView*header;

@end

@implementation FeedbackDetailTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{

    self = [super initWithFrame:frame style:style];
    
    if (self) {
        
        
        self.delegate = self;
        self.dataSource = self;
        self.backgroundColor = BGC;
        self.separatorColor = colordbdbdb;
        [self registerClass:[FeedbackDetailCell class] forCellReuseIdentifier:@"FeedbackDetailCell"];

        _header = [[OptionHeaderView alloc]initWithFrame:CGRectMake(0, 0, SIZEWIDTH, 110)];
        
        __block UIView *head = _header;
        __block UITableView *tab = self;
        _header.myBlock = ^(CGFloat height){
        
            tab.tableHeaderView = head;
        };
        
        [self setupRefresh];
        
    }
    return self;
}
/**
 *  数据加载
 *
 *  @param reload 是否重置数据
 */
- (void)isReloadData:(BOOL)reload{
    
    NSInteger pageIndex = 1;
    
    if (reload) {
        
    } else {

        pageIndex = [self sc_currentPageNumWithTotalCount:self.orders.count];
    }
    
    NSMutableDictionary *parms = [[NSMutableDictionary alloc]init];
    [parms setObject:@"815bffb68e2ea44e38cf16a98a5a29a4" forKey:@"oauth_token"];
    [parms setObject:@"8e84bc64f03d73b5c75406ef1d7349f7" forKey:@"oauth_token_secret"];
    [parms setObject:@10 forKey:@"num"];
    [parms setObject:[NSString stringWithFormat:@"%ld",(long)pageIndex] forKey:@"page"];
    [parms setObject:self.ztid forKey:@"ztid"];

    SCHttpRequest *request = [[SCHttpRequest alloc]initWithApi:API_detailFeedback params:parms];
    [self doPOSTConnect:request start:^(SCHttpRequest * _Nonnull req) {
        
    } success:^(SCHttpRequest * _Nonnull req, SCHttpResponse * _Nonnull resp) {
        
        NSDictionary *dic = resp.originalDictionary;
        
        NSArray *array = dic[@"data"][@"replylist"];
        
        OptionModel *model = [OptionModel modelWithDic:dic[@"data"]];
        
        [_header loadWithData:model];
        
        NSArray<FeedbackDetailModel*> *newArr = [FeedbackDetailModel mj_objectArrayWithKeyValuesArray:array];
        
        
        
        if (reload) {
            [self.orders removeAllObjects];
        }
        
        [self.orders addObjectsFromArray:newArr];
        
        [self reloadData];

    } failure:^(SCHttpRequest * _Nonnull req, NSError * _Nonnull error) {
        
    } finish:^(SCHttpRequest * _Nonnull req, BOOL success) {
        
        [self.mj_header endRefreshing];
        [self.mj_footer endRefreshing];
    }];

}
/**
 *  在没有内容的情况下获取内容
 */
- (void)loadDataOfNone{
    if (self.orders.count == 0) {

        [self isReloadData:YES];
    }
}

/**
 *  重置内部数据
 */
- (void)reset{
    
    [self.orders removeAllObjects];
    
    [self reloadData];
}
/**
 *  刷新加载
 */
- (void)setupRefresh{
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        [self.mj_header beginRefreshing];
        
        [self isReloadData:YES];
        
    }];
    header.stateLabel.textColor = COLOR_TITLE_999999;
    header.lastUpdatedTimeLabel.textColor = COLOR_TITLE_999999;
    self.mj_header = header;
    
    
    MJRefreshAutoNormalFooter * foot = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        [self.mj_footer beginRefreshing];
        
        [self isReloadData:NO];
    }];
    foot.stateLabel.textColor = COLOR_TITLE_999999;
    self.mj_footer = foot;
    
}

#pragma mark -- DataSource
- (NSInteger)numberOfRowsInSection:(NSInteger)section{
    if (self.orders.count>0) {
        return 1;
    }else{
        return 0;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.orders.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    FeedbackDetailModel *model = self.orders[indexPath.row];
    
    FeedbackDetailCell *cell = [FeedbackDetailCell cellWithTab:tableView];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    CGFloat height = [cell.commentLabel labelWithText:model.hfnr andNum:0];
    
    return ScaleSize(132)+21+height;
    
//    return 0;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

//    FeedbackDetailCell *cell = [FeedbackDetailCell cellWithTab:tableView];
    FeedbackDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FeedbackDetailCell"];

    [cell initWithModel:self.orders[indexPath.row]];
    
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.001f;
    }
    
    return 10.0f;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}
- (NSMutableArray<FeedbackDetailModel*>*)orders{
    
    if (!_orders) {
        _orders = [[NSMutableArray alloc]init];
    }
    return _orders;
}

@end
